package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.IArtistListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/11.
 * 艺术家列表控制层
 */
@Controller
@RequestMapping("/artistList")
public class ArtistListController {

    @Autowired
    private IArtistListService receiverListService;

    /**
     * 跳转到艺术家列表首页
     * @return
     */
    @RequestMapping(value = "/artistList",method = RequestMethod.GET)
    public String artistList(HttpServletRequest request,String keyword){

        try {
            //初始化数据
            Map<String,Object> initData = receiverListService.findInitData();

            request.setAttribute("initData",initData);
            request.setAttribute("keyword",keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "/receiver/artistList";
    }


    /**
     * 分页查询艺术家列表数据
     * @return
     */
    @RequestMapping(value = "/findArtistList", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findArtistList(@RequestParam Map param,HttpServletRequest request) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");

            PageHelper.startPage(page, rows);

            //Map<String,String> param = new HashMap<String, String>();

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = 0;
            if(userInfo != null){
                userID = userInfo.getUserId();
            }

            param.put("user_id",userID);

            List<Map<String,String>> list =  receiverListService.selectAllArtistList(param);

            pageInfo =  new PageInfo(list);


        } catch (Exception e) {

            e.printStackTrace();

//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }


        return pageInfo;

    }
}
